package com.qiyu.dubbo.router.starter.config;

import org.apache.dubbo.config.*;
import org.springframework.context.annotation.Configuration;

/**
 * @Author idea
 * @Date created in 2:28 下午 2020/11/27
 */
@Configuration
public class DubboCommonConfig {

    public static String ZK_ADDR = "127.0.0.1:2181";
    public static String NACOS_ADDR = "nacos://localhost:8848";

    public static <T> ReferenceConfig buildReferenceConfig(ApplicationConfig applicationConfig,
                                                           RegistryConfig registryConfig, Class<T> clazz){
        ReferenceConfig<T> referenceConfig=new ReferenceConfig<>();
        referenceConfig.setApplication(applicationConfig);
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setProtocol("duubo");
        referenceConfig.setInterface(clazz);
        return referenceConfig;
    }

    public static ProtocolConfig buildProtocolConfig(int port) {
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("dubbo");
        protocol.setPort(port);
        protocol.setThreads(200);
        return protocol;
    }

    public static RegistryConfig buildRegistryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(ZK_ADDR);
        return registryConfig;
    }

    public static ApplicationConfig buildApplication(String applicationName) {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(applicationName);
        return applicationConfig;
    }

    public static <T> ServiceConfig buildServiceConfig(ApplicationConfig applicationConfig,
                                                       RegistryConfig registryConfig,
                                                       ProtocolConfig protocolConfig,
                                                       Class clazz,
                                                       T ref ) {
        ServiceConfig<T> serviceServerConfig = new ServiceConfig<T>();
        serviceServerConfig.setApplication(applicationConfig);
        serviceServerConfig.setRegistry(registryConfig);
        serviceServerConfig.setProtocol(protocolConfig);
        serviceServerConfig.setInterface(clazz);
        serviceServerConfig.setRef((T) ref);
        return serviceServerConfig;
    }

}
